package cn.admin.common.exceptionInterceptor;

import cn.admin.common.domain.Result;
import cn.admin.common.exceptionInterceptor.exception.ServiceException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author YJ
 * @DateTime 2023/2/21 14:47
 */
@RestControllerAdvice
public class ExceptionInterceptor {

    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Result<String> bizErrorHandler(HttpServletRequest request, ServiceException serviceException) throws Exception {
        return Result.error(serviceException.getCode(),serviceException.getMessage());
    }
}
